﻿using Microsoft.Extensions.DependencyInjection;
using VIA.Integration;
using VIA.Integration.Alarms;
using VIA.Integration.Domain;
using VIA.Project.Domain.Services;
using VIA.SiMoJi.Domain.Electronics;
using VIA.SiMoJi.Domain.Option;

namespace VIA.SiMoJi.Domain.Inspection
{
    public class NGXiaLiao : Automatic, IHasOption<NGXiaLiaoStationOption>, ISuportInitialization, ISuportClearAlarm, IRefreshing
    {
        public volatile BanYunStation BanYunStation;
        private readonly string 安全位 = "安全位";
        private bool nGTrayError;
        private bool _isCylinderError;
        private bool jawSavePositionError;
        private InspectorOption _inspectorOption;

        [Alarm(true, AlarmLevel.Warning, "NG料盘信号异常", Logical.Equal)]
        public bool NGTrayError { get => nGTrayError; set => NotifyAlarm(ref nGTrayError, value); }

        public EventButton OpenButton { get; private set; }
        public EventButton CloesButton { get; private set; }

        public event EventHandler<int> StepsChanged;


        public NGXiaoLiaoRunningStatus Status;
        public Electronic Electronic => Electronic.Default;
        public CylinderEx C_无杆缸 => Electronic.Cylinder_NG下料无杆缸;
        public CylinderEx C_定位缸 => Electronic.Cylinder_NG下料抽屉定位气缸;

        public NGXiaLiaoStationOption Option { get; set; }

        public NGXiaLiao(EventButton openButton, EventButton closeButton)
        {
            this.InitalizeAlarms();
            OpenButton = openButton;
            CloesButton = closeButton;
            OpenButton.Pressed += DoorButton_Pressed;
            OpenButton.Released += DoorButton_Released;
            CloesButton.Pressed += CloesButton_Pressed;
        }

        private async void CloesButton_Pressed(object? sender, EventArgs e)
        {
            foreach (var item in BanYunStation.Z_Axises)
            {
                if (item.Current > item.GetTargetPosition(BanYunStation.Z_NG放料位))
                    return;
            }
            await C_无杆缸.TakeBackAsync();
            await C_定位缸.PushOutAsync();
        }

        private void DoorButton_Released(object? sender, EventArgs e)
        {
            Electronic.DO61_NG抽屉按钮灯.Value = false;
        }

        private async void DoorButton_Pressed(object? sender, EventArgs e)
        {
            foreach (var item in BanYunStation.Z_Axises)
            {
                if (item.Current > item.GetTargetPosition(BanYunStation.Z_NG放料位))
                    return;
            }
            Electronic.DO61_NG抽屉按钮灯.Value = true;
            await C_无杆缸.PushOutAsync();
            await C_定位缸.TakeBackAsync();
        }

        public async Task<bool> InitializeStation(CancellationToken token)
        {
            bool b = CheckIsTrayReady();
            return b;
        }

        /// <summary>
        /// 检测NG料盘就绪信号是否正常
        /// </summary>
        /// <returns></returns>
        public bool CheckIsTrayReady()
        {
           var isNGTrayReady = !Electronic.DI61_NG抽屉按钮.Value && Electronic.DI62_NG抽屉闭合光电.Value && !Electronic.DI63_NG抽屉打开光电.Value && Electronic.DI64_NG抽屉料盘有无光电.Value && Electronic.DI57_NG下料无杆缸缩回磁开.Value && !Electronic.DI58_NG下料无杆缸伸出磁开.Value && Electronic.DI59_NG下料抽屉定位缸伸出磁开.Value && !Electronic.DI60_NG下料抽屉定位缸缩回磁开.Value;
            return isNGTrayReady;
        }

        public async Task InitializeAsync()
        {
            //Status = RunningStatus.GetRecord<NGXiaoLiaoRunningStatus>("NGXiaoLiao.json");
            var repository = Service.GetRequiredService<IOptionRepository>();
            _inspectorOption = repository.Get<InspectorOption>();
        }

        public void Refresh()
        {
            OpenButton.Check();
            CloesButton.Check();
        }

        public void ClearAlarm()
        {
        }

        public void Refreshing()
        {
        }
    }
}
